package com.sa.token.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sa.token.dto.request.system.SystemLogRequest;
import com.sa.token.entity.SysOperationLogEntity;
import com.sa.token.mapper.SysOperationLogMapper;
import com.sa.token.message.Result;
import com.sa.token.message.StandardResponse;
import com.sa.token.service.SysOperationLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 操作记录表 服务实现类
 * </p>
 *
 * @author xhs
 * @since 2021-12-29
 */
@Service
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogMapper, SysOperationLogEntity> implements SysOperationLogService {

    /**
     * 分页查询系统操作日志日志
     *
     * @param logRequest
     * @return
     */
    @Override
    public StandardResponse<Object> getSystemLog(SystemLogRequest logRequest) {
        Page page = new Page<>(logRequest.getCurrent(), logRequest.getSize());
        LambdaQueryWrapper<SysOperationLogEntity> queryWrapper = new QueryWrapper<SysOperationLogEntity>().lambda()
                .like(StringUtils.hasLength(logRequest.getSystemModule()), SysOperationLogEntity::getSystemModule, logRequest.getSystemModule())
                .like(StringUtils.hasLength(logRequest.getLoginId()), SysOperationLogEntity::getCreator, logRequest.getLoginId())
                .like(StringUtils.hasLength(logRequest.getRequestApi()), SysOperationLogEntity::getRequestApi, logRequest.getRequestApi())
                .like(StringUtils.hasLength(logRequest.getResCode()), SysOperationLogEntity::getResCode, logRequest.getResCode())
                .ge(StringUtils.hasLength(logRequest.getStartTime()), SysOperationLogEntity::getCreateTime, logRequest.getStartTime())
                .le(StringUtils.hasLength(logRequest.getEndTime()), SysOperationLogEntity::getCreateTime, logRequest.getEndTime())
                .orderByDesc(SysOperationLogEntity::getCreateTime);
        Page list = getBaseMapper().selectPage(page, queryWrapper);
        return StandardResponse.build(Result.QUERY_SUCCESS).setData(list);
    }
}
